package com.lemon.components.ant.project.beans;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang3.StringUtils;

import com.lemon.components.ant.beans.ProjectInfo;
import com.lemon.components.ant.util.JdbcUtil;

public class ColumnInfo extends BaseInfo{

	public static String trim(String columnCode) {
		return StringUtils.replaceAll(columnCode, "`", "");
	}
	private String dbName;
	private String tableName;
	private String tableClazzCode;
	private String columnCode;
	private String columnRemark;
	private String columnType;
	private String dataType;
	private Long characterMaximumLength;
	private String javaCode;
	private String javaType;
	private boolean prikey;
	private boolean yesnoEnum;
	private boolean nullAble;
	private boolean number;
	private boolean date;
	private EnumInfo enumInfo;
	
	private ProjectInfo projectInfo;
	
	Pattern decimalPattern = Pattern.compile("decimal\\((\\d+),(\\d+)\\)",Pattern.CASE_INSENSITIVE);
	
	@Override
	public boolean equals(Object obj) {
		if (this == obj) {
			return true;
		}
		if (obj == null) {
			return false;
		}
		if (getClass() != obj.getClass()) {
			return false;
		}
		ColumnInfo other = (ColumnInfo) obj;
		if (javaCode == null) {
			if (other.javaCode != null) {
				return false;
			}
		} else if (!javaCode.equals(other.javaCode)) {
			return false;
		}
		return true;
	}
	
	public Long getCharacterMaximumLength() {
		return characterMaximumLength;
	}
	public String getColumnCode() {
		return columnCode;
	}
	public String getColumnRemark() {
		return columnRemark;
	}
	public String getColumnType() {
		return columnType;
	}
	public String getDataType() {
		return dataType;
	}
	public String getDbName() {
		return dbName;
	}
	public int getDecimalScale() {
		Matcher matcher = decimalPattern.matcher(columnType);
		if (matcher.find()) {
			return Integer.parseInt(matcher.group(2));
		}
		return -1;
	}
	public EnumInfo getEnumInfo() {
		return enumInfo;
	}
	public String getJavaCode() {
		return javaCode;
	}
	public String getJavaType() {
		if(dataType!=null && dataType.equals("enum") && yesnoEnum) {
			return "com.system.core.enums.YesOrNoEnum";
		} else if(dataType!=null && dataType.equals("enum")&&!yesnoEnum){
			return "com."+projectInfo.getCompanyName()+"."+projectInfo.getArtifactId()+".dal.mybatis.enums."+tableClazzCode+StringUtils.capitalize(javaCode) +"Enum";
		}else if(StringUtils.isNotBlank(javaType)){
			return javaType;
		} else {
			return JdbcUtil.typeCoverJdbc2Java(dataType);
		}
	}
	public ProjectInfo getProjectInfo() {
		return projectInfo;
	}
	public String getTableClazzCode() {
		return tableClazzCode;
	}
	public String getTableName() {
		return tableName;
	}
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((javaCode == null) ? 0 : javaCode.hashCode());
		return result;
	}
	public boolean isDate() {
		return date;
	}
	public boolean isNullAble() {
		return nullAble;
	}
	public boolean isNumber() {
		return number;
	}
	public boolean isPrikey() {
		return prikey;
	}
	public boolean isYesnoEnum() {
		return yesnoEnum;
	}
	public void setCharacterMaximumLength(Long characterMaximumLength) {
		this.characterMaximumLength = characterMaximumLength;
	}
	public void setColumnCode(String columnCode) {
		this.columnCode = columnCode;
	}
	public void setColumnRemark(String columnRemark) {
		this.columnRemark = columnRemark;
	}
	public void setColumnType(String columnType) {
		this.columnType = columnType;
	}
	public void setDataType(String dataType) {
		this.dataType = dataType;
	}
	public void setDate(boolean date) {
		this.date = date;
	}
	public void setDbName(String dbName) {
		this.dbName = dbName;
	}
	public void setEnumInfo(EnumInfo enumInfo) {
		this.enumInfo = enumInfo;
	}
	public void setJavaCode(String javaCode) {
		this.javaCode = javaCode;
	}
	public void setJavaType(String javaType) {
		this.javaType = javaType;
	}
	public void setNullAble(boolean nullAble) {
		this.nullAble = nullAble;
	}
	public void setNumber(boolean number) {
		this.number = number;
	}
	public void setPrikey(boolean prikey) {
		this.prikey = prikey;
	}
	public void setProjectInfo(ProjectInfo projectInfo) {
		this.projectInfo = projectInfo;
	}
	public void setTableClazzCode(String tableClazzCode) {
		this.tableClazzCode = tableClazzCode;
	}
	public void setTableName(String tableName) {
		this.tableName = tableName;
	}
	public void setYesnoEnum(boolean yesnoEnum) {
		this.yesnoEnum = yesnoEnum;
	}

	@Override
	public String toString() {
		return "ColumnInfo [dbName=" + dbName + ", tableName=" + tableName + ", tableClazzCode=" + tableClazzCode
				+ ", columnCode=" + columnCode + ", columnRemark=" + columnRemark + ", columnType=" + columnType
				+ ", dataType=" + dataType + ", characterMaximumLength=" + characterMaximumLength + ", javaCode="
				+ javaCode + ", javaType=" + javaType + ", prikey=" + prikey + ", yesnoEnum=" + yesnoEnum
				+ ", nullAble=" + nullAble + ", number=" + number + ", date=" + date + ", enumInfo=" + enumInfo
				+ ", projectInfo=" + projectInfo + ", decimalPattern=" + decimalPattern + "]";
	}
}
